package edu.jetwinner.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author xulixin
 */
public final class EasyMapUtil {

    private EasyMapUtil() {
        // reserved.
    }

    public static <K, V> boolean isEmpty(Map<K, V> map) {
        if (map == null) {
            return true;
        }
        return map.size() == 0;
    }

    public static <K, V> boolean isNotEmpty(Map<K, V> map) {
        return !isEmpty(map);
    }

    public static <K, V> HashMap<K, V> newHashMap() {
        return new HashMap<K, V>();
    }

    public static <K, V> HashMap<K, V> newHashMap(int initialCapacity) {
        return new HashMap<K, V>(initialCapacity);
    }

    public static final String OBJ_TO_MAP_KEY = "mass";

    @SuppressWarnings("unchecked")
    public static Map<String, Object> toMap(Object obj) {
        if (obj == null) {
            return new HashMap<>(0);
        }
        if (obj instanceof Map) {
            Map map = ((Map) obj);
            Map<String, Object> toMap = new HashMap<>(map);
            Set keys = map.keySet();
            Iterator it = keys.iterator();
            while (it.hasNext()) {
                Object k = it.next();
                toMap.put(String.valueOf(k), map.get(k));
            }
            return toMap;
        } else {
            Map<String, Object> map = new HashMap<>(1);
            map.put(OBJ_TO_MAP_KEY, obj);
            return map;
        }
    }
}
